package app.controllers.mobileweb;

import app.Const;
import app.events.EventBusContext;
import app.events.stat.MallActivityClickEvent;
import app.interceptors.MobileWxMemberInterceptor;
import app.models.activity.MallActivity;
import app.models.activity.MallActivityStat;
import app.models.activity.MallComment;
import app.models.activity.MallEnroll;
import app.models.member.Member;
import com.github.sog.config.StringPool;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.libs.AppFunc;
import com.google.common.base.Strings;
import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;

import java.util.List;

/**
 * <p>
 * .商场活动
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-24 10:42
 * @since JDK 1.6
 */
@Before(MobileWxMemberInterceptor.class)
public class MallActivityController extends BasicController {
    public void index() {
        List<MallActivity> activityList = MallActivity.dao.list();
        for (MallActivity activity : activityList) {
            String picture = activity.getStr("picture");
            if (!Strings.isNullOrEmpty(picture)) {
                //只取第一张活动图片
                final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(picture);
                String pic = pics.get(0);
                activity.set("picture", pic);
            }
        }
        setAttr("activityList", activityList);
        render("mall_activity_index.ftl");
    }

    public void showActivityDetal() {
        long aid = getParaToLong("aid", 0l);
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        if(aid > 0 && mid > 0) {
            MallEnroll enroll = MallEnroll.dao.getEnrollByMid(mid, aid);
            MallActivity activity = MallActivity.dao.findById(aid);
            if (null != activity && activity.getBoolean("state")) {
                String picture = activity.getStr("picture");
                if (!Strings.isNullOrEmpty(picture)) {
                    final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(picture);
                    String pic = pics.get(0);
                    activity.set("picture", pic);
                }
                setAttr("mid", mid);
                setAttr("notbeen", (null == enroll));//没参加
                setAttr("activity", activity);
                // click
                EventBusContext.post(new MallActivityClickEvent(aid));
                render("mall_activity_detal.ftl");
            }
        } else {
            renderNull();
        }
    }

    public void comments() {
        long activity = getParaToLong(0, 0l);
        if (activity > 0) {

            final List<MallComment> activityComments = MallComment.dao.findByActivity(activity);
            JsonKit.setDatePattern("yyyy年MM月dd日");
            renderJson(AjaxMessage.ok(activityComments));
            JsonKit.setDatePattern("yyyy-MM-dd");
        } else {
            renderJson(AjaxMessage.nodata());
        }
    }

    public void enrollPage() {
        String mid = getPara(Const.MEMBER_PARAM, "0");
        setAttr("aid", getPara("aid"));
        setAttr("mid", mid);
        render("enrolpage.ftl");
    }

    /**
     * 活动报名
     */
    public void enroll() {
        //获取信息
        String name = getPara("name");
        String phone = getPara("phone");
        String aid = getPara("aid");//活动id
        String mid = getPara("mid");

        MallEnroll me = MallEnroll.dao.getEnrollByNPA(name, phone, aid);
        if (null != me) {
            renderError("该活动你已经参加了");
            return;
        }
        //2插入数据
        boolean save = new MallEnroll().set("name", name)
                .set("phone", phone)
                .set("mall_activity_id", aid)
                .set("time", DateProvider.DEFAULT.getCurrentDate())
                .set("state", true)
                .set("member", mid)
                .save();

        if (save) {
            renderSuccess("恭喜你，报名成功！");
        } else {
            renderError("抱歉，请重新尝试报名！");
        }
    }

    /**
     * 活动评论
     */
    public void comment() {
        MallComment mc = new MallComment();
        String mid = getPara("mid", "0");
        Member member = Member.dao.findById(mid);
        if (null != member) {
            if (!member.getBoolean("temp")) {
                mc.set("cmt_name", member.getStr("name"));
                mc.set("cmt_avator", member.getStr("avator"));
            }
            mc.set("cmt_id", mid);
            mc.set("mall_activity", getPara("aid"));
            mc.set("cmt", getPara("comment"));
            mc.set("ctm_time", DateProvider.DEFAULT.getCurrentDate());
            boolean save = mc.save();
            if (!save) {
                renderError("评论失败！");
            } else {
                renderJson(AjaxMessage.ok("评论成功！", mc));
            }
        } else {
            renderError("评论失败！");
        }

    }
}
